home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Parser / grammar.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  4.4 KB  |  228 lines

  1. /* Grammar implementation */
  2.  
  3. #include "pgenheaders.h"
  4.  
  5. #include <ctype.h>
  6.  
  7. #include "assert.h"
  8. #include "token.h"
  9. #include "grammar.h"
  10.  
  11. extern int Py_DebugFlag;
  12.  
  13. grammar *
  14. newgrammar(start)
  15.     int start;
  16. {
  17.     grammar *g;
  18.     
  19.     g = PyMem_NEW(grammar, 1);
  20.     if (g == NULL)
  21.         Py_FatalError("no mem for new grammar");
  22.     g->g_ndfas = 0;
  23.     g->g_dfa = NULL;
  24.     g->g_start = start;
  25.     g->g_ll.ll_nlabels = 0;
  26.     g->g_ll.ll_label = NULL;
  27.     g->g_accel = 0;
  28.     return g;
  29. }
  30.  
  31. dfa *
  32. adddfa(g, type, name)
  33.     grammar *g;
  34.     int type;
  35.     char *name;
  36. {
  37.     dfa *d;
  38.     
  39.     PyMem_RESIZE(g->g_dfa, dfa, g->g_ndfas + 1);
  40.     if (g->g_dfa == NULL)
  41.         Py_FatalError("no mem to resize dfa in adddfa");
  42.     d = &g->g_dfa[g->g_ndfas++];
  43.     d->d_type = type;
  44.     d->d_name = name;
  45.     d->d_nstates = 0;
  46.     d->d_state = NULL;
  47.     d->d_initial = -1;
  48.     d->d_first = NULL;
  49.     return d; /* Only use while fresh! */
  50. }
  51.  
  52. int
  53. addstate(d)
  54.     dfa *d;
  55. {
  56.     state *s;
  57.     
  58.     PyMem_RESIZE(d->d_state, state, d->d_nstates + 1);
  59.     if (d->d_state == NULL)
  60.         Py_FatalError("no mem to resize state in addstate");
  61.     s = &d->d_state[d->d_nstates++];
  62.     s->s_narcs = 0;
  63.     s->s_arc = NULL;
  64.     s->s_lower = 0;
  65.     s->s_upper = 0;
  66.     s->s_accel = NULL;
  67.     s->s_accept = 0;
  68.     return s - d->d_state;
  69. }
  70.  
  71. void
  72. addarc(d, from, to, lbl)
  73.     dfa *d;
  74.     int lbl;
  75. {
  76.     state *s;
  77.     arc *a;
  78.     
  79.     assert(0 <= from && from < d->d_nstates);
  80.     assert(0 <= to && to < d->d_nstates);
  81.     
  82.     s = &d->d_state[from];
  83.     PyMem_RESIZE(s->s_arc, arc, s->s_narcs + 1);
  84.     if (s->s_arc == NULL)
  85.         Py_FatalError("no mem to resize arc list in addarc");
  86.     a = &s->s_arc[s->s_narcs++];
  87.     a->a_lbl = lbl;
  88.     a->a_arrow = to;
  89. }
  90.  
  91. int
  92. addlabel(ll, type, str)
  93.     labellist *ll;
  94.     int type;
  95.     char *str;
  96. {
  97.     int i;
  98.     label *lb;
  99.     
  100.     for (i = 0; i < ll->ll_nlabels; i++) {
  101.         if (ll->ll_label[i].lb_type == type &&
  102.             strcmp(ll->ll_label[i].lb_str, str) == 0)
  103.             return i;
  104.     }
  105.     PyMem_RESIZE(ll->ll_label, label, ll->ll_nlabels + 1);
  106.     if (ll->ll_label == NULL)
  107.         Py_FatalError("no mem to resize labellist in addlabel");
  108.     lb = &ll->ll_label[ll->ll_nlabels++];
  109.     lb->lb_type = type;
  110.     lb->lb_str = str; /* XXX strdup(str) ??? */
  111.     return lb - ll->ll_label;
  112. }
  113.  
  114. /* Same, but rather dies than adds */
  115.  
  116. int
  117. findlabel(ll, type, str)
  118.     labellist *ll;
  119.     int type;
  120.     char *str;
  121. {
  122.     int i;
  123.     
  124.     for (i = 0; i < ll->ll_nlabels; i++) {
  125.         if (ll->ll_label[i].lb_type == type /*&&
  126.             strcmp(ll->ll_label[i].lb_str, str) == 0*/)
  127.             return i;
  128.     }
  129.     fprintf(stderr, "Label %d/'%s' not found\n", type, str);
  130.     Py_FatalError("grammar.c:findlabel()");
  131.     return 0; /* Make gcc -Wall happy */
  132. }
  133.  
  134. /* Forward */
  135. static void translabel Py_PROTO((grammar *, label *));
  136.  
  137. void
  138. translatelabels(g)
  139.     grammar *g;
  140. {
  141.     int i;
  142.  
  143. #ifdef Py_DEBUG
  144.     printf("Translating labels ...\n");
  145. #endif
  146.     /* Don't translate EMPTY */
  147.     for (i = EMPTY+1; i < g->g_ll.ll_nlabels; i++)
  148.         translabel(g, &g->g_ll.ll_label[i]);
  149. }
  150.  
  151. static void
  152. translabel(g, lb)
  153.     grammar *g;
  154.     label *lb;
  155. {
  156.     int i;
  157.     
  158.     if (Py_DebugFlag)
  159.         printf("Translating label %s ...\n", PyGrammar_LabelRepr(lb));
  160.     
  161.     if (lb->lb_type == NAME) {
  162.         for (i = 0; i < g->g_ndfas; i++) {
  163.             if (strcmp(lb->lb_str, g->g_dfa[i].d_name) == 0) {
  164.                 if (Py_DebugFlag)
  165.                     printf(
  166.                         "Label %s is non-terminal %d.\n",
  167.                         lb->lb_str,
  168.                         g->g_dfa[i].d_type);
  169.                 lb->lb_type = g->g_dfa[i].d_type;
  170.                 lb->lb_str = NULL;
  171.                 return;
  172.             }
  173.         }
  174.         for (i = 0; i < (int)N_TOKENS; i++) {
  175.             if (strcmp(lb->lb_str, _PyParser_TokenNames[i]) == 0) {
  176.                 if (Py_DebugFlag)
  177.                     printf("Label %s is terminal %d.\n",
  178.                         lb->lb_str, i);
  179.                 lb->lb_type = i;
  180.                 lb->lb_str = NULL;
  181.                 return;
  182.             }
  183.         }
  184.         printf("Can't translate NAME label '%s'\n", lb->lb_str);
  185.         return;
  186.     }
  187.     
  188.     if (lb->lb_type == STRING) {
  189.         if (isalpha((int)(lb->lb_str[1])) || lb->lb_str[1] == '_') {
  190.             char *p;
  191.             if (Py_DebugFlag)
  192.                 printf("Label %s is a keyword\n", lb->lb_str);
  193.             lb->lb_type = NAME;
  194.             lb->lb_str++;
  195.             p = strchr(lb->lb_str, '\'');
  196.             if (p)
  197.                 *p = '\0';
  198.         }
  199.         else if (lb->lb_str[2] == lb->lb_str[0]) {
  200.             int type = (int) PyToken_OneChar(lb->lb_str[1]);
  201.             if (type != OP) {
  202.                 lb->lb_type = type;
  203.                 lb->lb_str = NULL;
  204.             }
  205.             else
  206.                 printf("Unknown OP label %s\n",
  207.                     lb->lb_str);
  208.         }
  209.         else if (lb->lb_str[2] && lb->lb_str[3] == lb->lb_str[0]) {
  210.             int type = (int) PyToken_TwoChars(lb->lb_str[1],
  211.                            lb->lb_str[2]);
  212.             if (type != OP) {
  213.                 lb->lb_type = type;
  214.                 lb->lb_str = NULL;
  215.             }
  216.             else
  217.                 printf("Unknown OP label %s\n",
  218.                     lb->lb_str);
  219.         }
  220.         else
  221.             printf("Can't translate STRING label %s\n",
  222.                 lb->lb_str);
  223.     }
  224.     else
  225.         printf("Can't translate label '%s'\n",
  226.                PyGrammar_LabelRepr(lb));
  227. }
  228.